import Vue from 'vue'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)

import zh from './config/zh'
import en from './config/en'

let language = uni.getStorageSync('language')
if (!language) {
  language = uni.getSystemInfoSync().language
  uni.setStorageSync('language', language)
}

export const i18n = new VueI18n({
  locale: language,
  silentTranslationWarn: ['production'].includes(process.env.NODE_ENV),
  messages: {
    zh,
    en,
  },
})

// 设置 Tabbar 语言
export function setTabbarLang() {
  uni.setTabBarItem({
    index: 0,
    text: i18n.t('tabbar.home'),
  })
  uni.setTabBarItem({
    index: 1,
    text: i18n.t('tabbar.mine'),
  })
}

// 切换语言
export function changeLanguage(language) {
  i18n.locale = language
  uni.setStorageSync('language', i18n.locale)
  setTabbarLang()

  uni.$emit('setMescrollGlobalOption', {
    i18n: {
      type: i18n.locale,
    },
  })
}
